package cn.raven.system.controller;

import cn.raven.basic.annotation.PreAuthorize;
import cn.raven.system.service.IRoleService;
import cn.raven.system.domain.Role;
import cn.raven.system.query.RoleQuery;
import cn.raven.basic.utils.AjaxResult;
import cn.raven.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    public IRoleService roleService;


    /**
     * 保存和修改公用的
     * @param role  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存角色",sn = "role:save")
    public AjaxResult save(@RequestBody Role role){
        try {
            if( role.getId()!=null)
                roleService.update(role);
            else
                roleService.add(role);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除角色",sn = "role:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            roleService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "id查询角色",sn = "role:id")
    public Role findById(@PathVariable("id")Long id)
    {
        return roleService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Role> findAll(){

        return roleService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "角色列表",sn = "role:list")
    public PageList<Role> queryData(@RequestBody RoleQuery query)
    {
        return roleService.queryData(query);
    }

    /**
     * @Title: batchDel
     * @Description: 根据ids 批量删除
     * @Author: king
     * @Version: 1.0
     * @Date:  2022/6/25 11:11
     * @Parameters: [ids]
     * @Return cn.raven.basic.utils.AjaxResult
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除角色",sn = "role:batchDelete")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            roleService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
}
